package cn.sc.allinpay.allfavour.business.setremark;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;
import android.widget.LinearLayout;

import butterknife.BindView;
import butterknife.OnClick;
import cn.pai.common.listener.OnPromptListener;
import cn.sc.allinpay.allfavour.R;
import cn.sc.allinpay.allfavour.base.activity.BaseActivity;
import cn.sc.allinpay.allfavour.business.common.widget.CustomRemarkView;

/**
 * 设置验券备注
 */
public class SetRemarkActivity extends BaseActivity<SetRemarkContract.View, SetRemarkContract.Presenter> implements SetRemarkContract.View {

    @BindView(R.id.llContent)
    LinearLayout llContent;

    @Override
    protected int layoutId() {
        return R.layout.activity_set_remark;
    }

    @Override
    protected void layoutTitle(View top, AppCompatTextView left, AppCompatTextView title, AppCompatTextView right) {
        title.setText("验券备注");
        right.setText("保存");
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuffer buffer = new StringBuffer();
                for (int i = 0; i < llContent.getChildCount(); i++) {
                    CustomRemarkView remarkView = (CustomRemarkView) llContent.getChildAt(i);
                    String remark = remarkView.getRemark();
                    if (!TextUtils.isEmpty(remark)) {
                        buffer.append(remark).append("!");
                    }
                }
                if (buffer.length() > 0) {
                    presenter.saveRemarks(buffer.substring(0, buffer.length() - 1));
                } else {
                    presenter.saveRemarks("");
                }
            }
        });
    }

    @Override
    protected SetRemarkContract.Presenter bindPresenter() {
        return new SetRemarkPresenter();
    }

    @Override
    protected void layoutContent(Bundle savedInstanceState) {


    }

    public void addRemarkView(String remark) {
        CustomRemarkView remarkView = new CustomRemarkView(this);
        remarkView.setRemark(remark);
        remarkView.setOnRemarkListener(new CustomRemarkView.OnRemarkListener() {
            @Override
            public void onDelete(final View view) {
                prompt(false, "是否删除该备注信息?", new OnPromptListener("是", "否") {

                    @Override
                    public void onPositive(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        llContent.removeView(view);
                    }

                    @Override
                    public void onNegative(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
            }
        });
        llContent.addView(remarkView);
    }

    @OnClick(R.id.btnAdd)
    public void click(View view) {
        switch (view.getId()) {
            case R.id.btnAdd:
                addRemarkView(null);
                break;
        }
    }

    @Override
    public void showRemarkView(String[] remarks) {
        for (int i = 0; i < remarks.length; i++) {
            String remark = remarks[i];
            addRemarkView(remark);
        }
    }
}

